package ops.view;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JPanel;

import ops.controller.ControllerManager;
import ops.controller.TermController;
import ops.i18n.Resources;
import ops.util.ViewHelper;
import ops.view.forms.Form;
import ops.view.forms.component.NumberInput;
import ops.view.tables.Column;
import ops.view.tables.TableMaker;

public class TermRegisterPanel extends JPanel implements Refreshable {

	private TermController termController = ControllerManager.getInstance().getTermController();
	
	private JPanel formPanel;

	private JPanel buttonsPanel;
	private JButton newButton;
	private JButton saveButton;
	private JButton cancelButton;

	private TableMaker tableMaker;
	private Form form;

	public TermRegisterPanel() {
		super(new BorderLayout());

		init();
	}

	public void clearTable() {
		tableMaker.clearTable();
	}
	
	public void addRowToTable(Object... data) {
		tableMaker.addRow(data);
	}

	private void init() {
		formPanel = new JPanel(new BorderLayout());

		initButtons();
		initForm();
		initTable();

		add(formPanel, BorderLayout.SOUTH);
	}

	private void initTable() {
		Resources resources = Resources.getInstance();

		tableMaker = new TableMaker();
		tableMaker.addColumn(new Column(resources.getString("term.year"), Integer.class));
		tableMaker.addColumn(new Column(resources.getString("term.semester"), Integer.class));
		
		add(ViewHelper.makeScrollPane(tableMaker.getTable()), BorderLayout.CENTER);
	}
	
	private void initButtons() {
		buttonsPanel = new JPanel(new FlowLayout(FlowLayout.CENTER, 5, 5));

		ButtonListener listener = new ButtonListener();

		saveButton = ViewHelper.makeSaveButton();
		saveButton.addActionListener(listener);
		newButton = ViewHelper.makeNewButton();
		newButton.addActionListener(listener);
		cancelButton = ViewHelper.makeCancelButton();
		cancelButton.addActionListener(listener);

		buttonsPanel.add(newButton);
		buttonsPanel.add(saveButton);
		buttonsPanel.add(cancelButton);

		formPanel.add(buttonsPanel, BorderLayout.CENTER);
	}

	private void initForm() {
		Resources resources = Resources.getInstance();

		form = new Form();
		form.addInput("term.year", new NumberInput(resources.getString("term.year")+ ":", 10, true));
		form.addInput("term.semester", new NumberInput(resources.getString("term.semester")+ ":", 10, true));

		formPanel.add(form.getPanel(), BorderLayout.SOUTH);
	}

	private class ButtonListener implements ActionListener {
		public void actionPerformed(ActionEvent e) {
			if (!(e.getSource() instanceof JButton)) {
				return;
			}
			
			JButton button = (JButton) e.getSource();
			
			if (!button.isEnabled()) {
				return;
			}
			
			if (button == saveButton) {
				Integer year = form.getIntegerFor("term.year");
				Integer semester = form.getIntegerFor("term.semester");
				
				if (form.validate()) {
					termController.add(year, semester);
					
					saveButton.setEnabled(false);
					newButton.setEnabled(true);
					
					form.clear();
				} else {
					ViewHelper.showFormValidationError();
					return;
				}
			} else if (button == newButton) {
				form.clear();
				tableMaker.clearSelection();
				
				saveButton.setEnabled(true);
				newButton.setEnabled(false);
			} else if (button == cancelButton) {
				form.clear();
				tableMaker.clearSelection();
				
				saveButton.setEnabled(false);
				newButton.setEnabled(true);
			}
		}
	}
	
	public void refresh() {
		termController.updateTable();
	}
}